package com.jielihaofeng.netty.handlerpipeline;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Johnnie Wind
 * @description
 * @date 2021/11/10 10:15
 */
@Slf4j
public class TestPipeline {

    public static void main(String[] args) {
        Student student = new Student("wzy");

        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("h1", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("1");
                                // 将数据传递给下个 handler，如果不调用，调用链会断开 或者调用 ctx.fireChannelRead(student);
//                                ctx.fireChannelRead(student);
                                super.channelRead(ctx, student.name);
                                //  ctx.fireChannelRead(msg);或super.channelRead(ctx, msg); 两者必须调一个，把数据传给下一个入站处理器
                            }
                        });
                        pipeline.addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("2");
                                Student s = new Student(msg.toString());
                                super.channelRead(ctx, s);
                            }
                        });
                        pipeline.addLast("h3", new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("3");

                                // 从尾部开始找出站处理器，输出123654
                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes(msg.toString().getBytes()));
                                // 从当前节点找上一个出站处理器，输出123，因为3之前没有其他出站处理器
                                // ctx.writeAndFlush(ctx.alloc().buffer().writeBytes("server...".getBytes()));
                            }
                        });

                        // 出站处理器，向通道写了数据才会触发
                        pipeline.addLast("h4", new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("4");
                                super.write(ctx, msg, promise);
                            }
                        });
                        pipeline.addLast("h5", new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("5");
                                super.write(ctx, msg, promise);
                            }
                        });
                        pipeline.addLast("h6", new ChannelOutboundHandlerAdapter(){
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("6");
//                                super.write(ctx, msg, promise);
                                ctx.write(msg,promise);
                                // 改成这样会输出123666，ctx.channel().write(msg)从尾部开始查找，结果尾部又是自己
                                // ctx.channel().write(msg);
                            }
                        });
                    }
                })
                .bind(8088);
    }

    @Data
    @AllArgsConstructor
    static class Student {
        private String name;
    }
}
